package com.tensquare.mq.custom;

import com.tensquare.mq.cofig.MqConfig;
import lombok.extern.log4j.Log4j2;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 需要测试消息是否创建 可以注释掉@Component注解然后在页面查看消息 192.168.145.135:15672
 * 也可以在控制器测试手动消费消息
 */
@Component
@Log4j2
public class Customer1 {


    /**
     * 监听 分裂模式的交换机和所有绑定到分裂模式的队列
     * @param massage
     */
    @RabbitListener(
            bindings =
                    {
                            @QueueBinding(value = @Queue(value = MqConfig.FANOUT_QUEUE_NAME, durable = "true"),
                                    exchange = @Exchange(value = MqConfig.TEST_FANOUT_EXCHANGE, type = "fanout"))
                    })
    @RabbitHandler
    public void processFanoutMsg(Message massage) {
        String msg = new String(massage.getBody(), StandardCharsets.UTF_8);
        log.info("received Fanout message : " + msg);
    }

    @RabbitListener(
            bindings =
                    {
                            @QueueBinding(value = @Queue(value = MqConfig.FANOUT_QUEUE_NAME_2, durable = "true"),
                                    exchange = @Exchange(value = MqConfig.TEST_FANOUT_EXCHANGE, type = "fanout"))
                    })
    @RabbitHandler
    public void processFanout1Msg(Message massage) {
        String msg = new String(massage.getBody(), StandardCharsets.UTF_8);
        log.info("received Fanout1 message : " + msg);
    }


    /**
     *   durable 指定是否持久化
     * @param massage
     */
    @RabbitListener(
            bindings =
                    {
                            @QueueBinding(value = @Queue(value = MqConfig.DIRECT_QUEUE_NAME, durable = "true"),
                                    exchange = @Exchange(value = MqConfig.TEST_DIRECT_EXCHANGE),
                                    key = MqConfig.DIRECT_ROUTINGKEY)
                    })
    @RabbitHandler
    public void processDirectMsg(Message massage) {
        String msg = new String(massage.getBody(), StandardCharsets.UTF_8);
        log.info("received Direct message : " + msg);
    }

    @RabbitListener(
            bindings =
                    {
                            @QueueBinding(value = @Queue(value = MqConfig.TOPIC_QUEUE_NAME, durable = "true"),
                                    exchange = @Exchange(value = MqConfig.TEST_TOPIC_EXCHANGE, type = "topic"),
                                    key = MqConfig.TOPIC_ROUTINGKEY)
                    })
    @RabbitHandler
    public void processTopicMsg(Message massage) {
        String msg = new String(massage.getBody(), StandardCharsets.UTF_8);
        log.info("received Topic message : " + msg);
    }


}
